
package com.dps.omc.service.impl;
import com.dps.omc.model.Subject;
import com.dps.omc.model.SubjectBizType;
import com.dps.omc.model.SubjectBizUnit;
import com.dps.omc.service.ISubjectArtifactService;
import com.dps.omc.service.ISubjectBizTypeService;
import com.dps.omc.service.ISubjectBizUnitService;
import com.dps.common.utils.IdGenerator;
import com.dps.omc.dao.ISubjectDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dps.common.service.impl.AbstractPageService;
import com.dps.omc.service.ISubjectService;

import java.util.List;

@SuppressWarnings("unchecked")
@Service("SubjectServiceImpl")

public class SubjectServiceImpl extends AbstractPageService<ISubjectDAO,Subject> implements ISubjectService {

	@Autowired
	private ISubjectDAO subjectDAO;
	@Autowired
	private ISubjectArtifactService subjectArtifactService;
	@Autowired
	private ISubjectBizTypeService subjectBizTypeService;
	@Autowired
	private ISubjectBizUnitService subjectBizUnitService;
	@Override
	public boolean getEnableDataPerm() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public ISubjectDAO getDao() {
		return subjectDAO;
	}

	@Override
	public void create(Subject subject, List<String> bizTypes, List<String> bizUnits) {
		this.add(subject);
		for(String bizType:bizTypes)
		{
			SubjectBizType subjectBizType = new SubjectBizType();
			subjectBizType.setId(IdGenerator.createNewId());
			subjectBizType.setSubjectId(subject.getId());
			subjectBizType.setBizType(bizType);
			subjectBizTypeService.add(subjectBizType);
		}
		for(String bizUnit:bizUnits)
		{
			SubjectBizUnit subjectBizUnit = new SubjectBizUnit();
			subjectBizUnit.setId(IdGenerator.createNewId());
			subjectBizUnit.setSubjectId(subject.getId());
			subjectBizUnit.setBizUnit(bizUnit);
			subjectBizUnitService.add(subjectBizUnit);
		}

	}

	@Override
	public void update(Subject subject, List<String> bizTypes, List<String> bizUnits) {
		this.update(subject);
		subjectBizTypeService.deleteByProperty("subjectId",subject.getId());
		for(String bizType:bizTypes)
		{
			SubjectBizType subjectBizType = new SubjectBizType();
			subjectBizType.setId(IdGenerator.createNewId());
			subjectBizType.setSubjectId(subject.getId());
			subjectBizType.setBizType(bizType);
			subjectBizTypeService.add(subjectBizType);
		}
		subjectBizUnitService.deleteByProperty("subjectId",subject.getId());
		for(String bizUnit:bizUnits)
		{
			SubjectBizUnit subjectBizUnit = new SubjectBizUnit();
			subjectBizUnit.setId(IdGenerator.createNewId());
			subjectBizUnit.setSubjectId(subject.getId());
			subjectBizUnit.setBizUnit(bizUnit);
			subjectBizUnitService.add(subjectBizUnit);
		}

	}

	@Override
	public List<Subject> editStatus(String sid) {
		return getDao().editStatus(sid);
	}
}
